/**
 * Copyright(C) 2012 Luvina
 * ListUserManaged.java, 25-05-2012 NguyenVietDung
 */
package net.luvina.manageuser.managedbean;

import java.io.Serializable;
import java.util.List;

import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import javax.faces.event.PhaseId;
import javax.faces.model.SelectItem;
import javax.servlet.http.HttpServletRequest;

import net.luvina.manageuser.constant.CommonConstant;
import net.luvina.manageuser.dto.UserDetailDto;
import net.luvina.manageuser.logic.MstGroupLogic;
import net.luvina.manageuser.logic.TblUserLogic;
import net.luvina.manageuser.util.CommonUtil;
import net.luvina.manageuser.util.ConfigProperties;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * ListUserManaged: Controller để xử lý cho màn hình ADM002
 *
 * @author NguyenVietDung
 *
 */
@Component("listUserManaged")
@Scope("session")
public class ListUserManaged extends UserBaseManaged implements Serializable {
	private int currentSection;
	private int totalSection;
	private int currentPage;
	private String fullName;
	private int groupId;
	private boolean isBack;
	private List<SelectItem> itemGroups;
	private List<UserDetailDto> lsUserDetailDto;
	private int offset;
	private int limit;
	private String message;
	private static final long serialVersionUID = 1L;
	private String sortByCodeLevel;
	private String sortByEndDate;
	private String sortByFullName;
	private String sortType;
	public static final int STEP = 3;
	private List<Integer> strPaging;
	private int totalRecord;

	@Autowired
	private TblUserLogic tblUserLogic;
	@Autowired
	private MstGroupLogic mstGroupLogic;

	/**
 *
 */
	public ListUserManaged() {
		// TODO Auto-generated constructor stub
		this.setValueDefault();
	}

	/**
	 *
	 * initPage
	 *
	 * @param event
	 *            [Hàm chạy đầu tiên của JSF]
	 */
	public final void initPage(PhaseEvent event) {
		if (event.getPhaseId() == PhaseId.RENDER_RESPONSE) {
			// Add data for comboBox Group
			this.itemGroups = this.mstGroupLogic.getListItemGroup("全て");
			this.doSearch();
		}
	}

	/**
	 * doSearch [Xử lý logic search của màn hình ADM002]
	 *
	 * @return String ADM002 [Chuyển sang màn hình ADM002]
	 */
	public final String doSearch() {
		System.out.println("============== Begin doSearch ListUserManaged ==============");
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		String strPaging = request.getParameter("pageNumber");
		String nextSectionStr = request.getParameter("nextSection");
		String previousSectionStr = request.getParameter("previousSection");
		int pageSection = Integer.parseInt(ConfigProperties.getData("pageSection"));
		this.limit = CommonUtil.getLimit();
		this.offset = CommonUtil.getOffset(currentPage, limit);
		this.totalRecord = this.tblUserLogic.getTotalRecords(groupId, fullName);
		if (previousSectionStr == null || nextSectionStr == null || strPaging == null) {
			this.currentSection = 1;
			this.currentPage = 1;
		}
		if (previousSectionStr != null && strPaging == null) {
			this.currentSection = Integer.parseInt(previousSectionStr);
		}
		if (nextSectionStr != null && strPaging == null) {
			this.currentSection = Integer.parseInt(nextSectionStr);
		}
		// Like minPageSection in CommonUtil
		this.currentPage = this.currentSection * pageSection - (pageSection - 1);

		if (strPaging != null) {
			this.currentPage = Integer.parseInt(strPaging);
			this.currentSection = CommonUtil.getCurrentSection(this.currentPage, pageSection);
		}
		if (this.totalRecord > 0) {
			this.lsUserDetailDto = tblUserLogic.getListUser(this.offset, this.limit, this.groupId, this.fullName, this.sortType, this.sortByFullName,
					this.sortByCodeLevel, this.sortByEndDate);
			int totalPages = CommonUtil.getTotalPage(this.totalRecord, this.limit);
			this.totalSection = CommonUtil.getTotalSection(totalPages, pageSection);
			this.strPaging = CommonUtil.getListPaging(this.currentSection, pageSection, totalPages);
			this.message = CommonConstant.EMPTY_STRING;
		} else {
			this.setValueDefault();
			this.lsUserDetailDto = null;
			this.strPaging = null;
			this.message = CommonConstant.MSG005;
		}
		System.out.println("--------" + this.currentSection);
		System.out.println("--------" + this.totalSection);
		System.out.println("============== End doSearch ListUserManaged ==============");
		return CommonConstant.ADM002;
	}

	/**
	 *
	 * doSort [Xử lý logic sort của màn hình ADM002]
	 *
	 * @return String ADM002 [Chuỗi trả về để chuyển sang màn hình ADM002]
	 */
	public final String doSort() {
		HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
		String strSortType = request.getParameter("sortType");
		if ("full_name".equalsIgnoreCase(strSortType)) {
			this.sortType = strSortType;
			if ("ASC".equalsIgnoreCase(request.getParameter("sortByFullName"))) {
				this.setValueDefault();
				this.sortByFullName = "DESC";
			} else {
				this.setValueDefault();
				this.sortByFullName = "ASC";
			}
		} else if ("code_level".equalsIgnoreCase(strSortType)) {
			this.sortType = strSortType;
			if ("ASC".equalsIgnoreCase(request.getParameter("sortByCodeLevel"))) {
				this.setValueDefault();
				this.sortByCodeLevel = "DESC";
			} else {
				this.setValueDefault();
				this.sortByCodeLevel = "ASC";
			}

		} else if ("end_date".equalsIgnoreCase(strSortType)) {
			this.sortType = strSortType;
			if ("ASC".equalsIgnoreCase(request.getParameter("sortByEndDate"))) {
				this.setValueDefault();
				this.sortByEndDate = "DESC";
			} else {
				this.setValueDefault();
				this.sortByEndDate = "ASC";
			}
		} else {
			this.sortType = "full_name";
			setValueDefault();
		}
		return CommonConstant.ADM002;
	}

	/**
	 *
	 * doInit [Xử lý logic khi click button Back ở các màn hình khác về ADM002 ]
	 *
	 * @return String AMD002 [Chuỗi trả về để chuyển sang màn hình ADM002]
	 */
	public final String doInit() {

		return CommonConstant.ADM002;
	}

	/**
	 *
	 * doBack: Xử lý logic khi click button Back ở các màn hình khác về ADM002
	 *
	 * @return String AMD002 [Chuỗi trả về để chuyển sang màn hình ADM002]
	 */
	public final String doBack() {

		return CommonConstant.ADM002;
	}

	/**
	 *
	 * setValueDefault [Set giá trị search default cho màn hình ADM002]
	 *
	 * @param [Param Description]
	 */

	private final void setValueDefault() {
		this.sortByFullName = "ASC";
		this.sortByCodeLevel = "ASC";
		this.sortByEndDate = "DESC";
		this.currentPage = 1;
		this.currentSection = 1;
		this.sortType = "full_name";
	}

	/**
	 * @return the currentPage
	 */
	public int getCurrentPage() {
		return currentPage;
	}

	/**
	 * @param currentPage
	 *            the currentPage to set
	 */
	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	/**
	 * @return the fullName
	 */
	public String getFullName() {
		return fullName;
	}

	/**
	 * @param fullName
	 *            the fullName to set
	 */
	public void setFullName(String fullName) {
		this.fullName = fullName;
	}

	/**
	 * @return the groupId
	 */
	public int getGroupId() {
		return groupId;
	}

	/**
	 * @param groupId
	 *            the groupId to set
	 */
	public void setGroupId(int groupId) {
		this.groupId = groupId;
	}

	/**
	 * @return the isBack
	 */
	public boolean isBack() {
		return isBack;
	}

	/**
	 * @param isBack
	 *            the isBack to set
	 */
	public void setBack(boolean isBack) {
		this.isBack = isBack;
	}

	/**
	 * @return the itemGroups
	 */
	public List<SelectItem> getItemGroups() {
		return itemGroups;
	}

	/**
	 * @param itemGroups
	 *            the itemGroups to set
	 */
	public void setItemGroups(List<SelectItem> itemGroups) {
		this.itemGroups = itemGroups;
	}

	/**
	 * @return the offset
	 */
	public int getOffset() {
		return offset;
	}

	/**
	 * @param offset
	 *            the offset to set
	 */
	public void setOffset(int offset) {
		this.offset = offset;
	}

	/**
	 * @return the limit
	 */
	public int getLimit() {
		return limit;
	}

	/**
	 * @param limit
	 *            the limit to set
	 */
	public void setLimit(int limit) {
		this.limit = limit;
	}

	/**
	 * @return the message
	 */
	public String getMessage() {
		return message;
	}

	/**
	 * @param message
	 *            the message to set
	 */
	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * @return the sortByCodeLevel
	 */
	public String getSortByCodeLevel() {
		return sortByCodeLevel;
	}

	/**
	 * @param sortByCodeLevel
	 *            the sortByCodeLevel to set
	 */
	public void setSortByCodeLevel(String sortByCodeLevel) {
		this.sortByCodeLevel = sortByCodeLevel;
	}

	/**
	 * @return the sortByEndDate
	 */
	public String getSortByEndDate() {
		return sortByEndDate;
	}

	/**
	 * @param sortByEndDate
	 *            the sortByEndDate to set
	 */
	public void setSortByEndDate(String sortByEndDate) {
		this.sortByEndDate = sortByEndDate;
	}

	/**
	 * @return the sortByFullName
	 */
	public String getSortByFullName() {
		return sortByFullName;
	}

	/**
	 * @param sortByFullName
	 *            the sortByFullName to set
	 */
	public void setSortByFullName(String sortByFullName) {
		this.sortByFullName = sortByFullName;
	}

	/**
	 * @return the sortType
	 */
	public String getSortType() {
		return sortType;
	}

	/**
	 * @param sortType
	 *            the sortType to set
	 */
	public void setSortType(String sortType) {
		this.sortType = sortType;
	}

	/**
	 * @return the strPaging
	 */
	public List<Integer> getStrPaging() {
		return strPaging;
	}

	/**
	 * @param strPaging
	 *            the strPaging to set
	 */
	public void setStrPaging(List<Integer> strPaging) {
		this.strPaging = strPaging;
	}

	/**
	 * @return the totalRecord
	 */
	public int getTotalRecord() {
		return totalRecord;
	}

	/**
	 * @param totalRecord
	 *            the totalRecord to set
	 */
	public void setTotalRecord(int totalRecord) {
		this.totalRecord = totalRecord;
	}

	/**
	 * @return the tblUserLogic
	 */
	public TblUserLogic getTblUserLogic() {
		return tblUserLogic;
	}

	/**
	 * @param tblUserLogic
	 *            the tblUserLogic to set
	 */
	public void setTblUserLogic(TblUserLogic tblUserLogic) {
		this.tblUserLogic = tblUserLogic;
	}

	/**
	 * @return the mstGroupLogic
	 */
	public MstGroupLogic getMstGroupLogic() {
		return mstGroupLogic;
	}

	/**
	 * @param mstGroupLogic
	 *            the mstGroupLogic to set
	 */
	public void setMstGroupLogic(MstGroupLogic mstGroupLogic) {
		this.mstGroupLogic = mstGroupLogic;
	}

	/**
	 * @return the serialversionuid
	 */
	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	/**
	 * @return the step
	 */
	public static int getStep() {
		return STEP;
	}

	/**
	 * @return the lsUserDetailDto
	 */
	public List<UserDetailDto> getLsUserDetailDto() {
		return lsUserDetailDto;
	}

	/**
	 * @param lsUserDetailDto
	 *            the lsUserDetailDto to set
	 */
	public void setLsUserDetailDto(List<UserDetailDto> lsUserDetailDto) {
		this.lsUserDetailDto = lsUserDetailDto;
	}

	/**
	 * @return the currentSection
	 */
	public int getCurrentSection() {
		return currentSection;
	}

	/**
	 * @param currentSection
	 *            the currentSection to set
	 */
	public void setCurrentSection(int currentSection) {
		this.currentSection = currentSection;
	}

	/**
	 * @return the totalSection
	 */
	public int getTotalSection() {
		return totalSection;
	}

	/**
	 * @param totalSection
	 *            the totalSection to set
	 */
	public void setTotalSection(int totalSection) {
		this.totalSection = totalSection;
	}

}
